package com.zyq.service.impl;

import com.zyq.mapper.SignInMapper;
import com.zyq.service.ISignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: zyq
 * @create: 2022-03-04
 **/
@Service
public class SignInImpl implements ISignInService {

    @Autowired
    private SignInMapper signInMapper;


    /**
     * 插入签到表
     * @param userId
     * @param topicId
     * @return
     */
    @Override
    public int insertSignIn(Long userId,Long topicId){
        return signInMapper.insertSignIn(userId, topicId);
    }
    /**
     * 插入签到表
     * @param userId
     * @param topicId
     * @return
     */
    @Override
    public int countSignIn(Long userId, Long topicId){
        return signInMapper.countSignIn(userId, topicId);
    }

    /**
     * 统计签到次数
     */
    @Override
    public int getTimes(Long userId){
         List<Date> times = signInMapper.getTimes(userId);
        int continuousDays = 1;
        Date dNow = new Date();   //当前时间
        for(int i = 0 ; i < times.size() ; i ++) {
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.setTime(dNow);//把当前时间赋给日历
            calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
            dNow = calendar.getTime();   //得到前一天的时间
            DateFormat df1 = DateFormat.getDateInstance();
            if(df1.format(dNow).equals(df1.format(times.get(i)))){
                continuousDays++;
            }else{
                break;
            }
        }
        return continuousDays;

    }
}
